package com.cykj.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JWTUtils {
    // 秘钥
    private static final String signKey = "test1234test1234test1234test1234test1234test1234";
    private static int expireTime=24*6*6*1000;

    public static String createJWT(Map<String, Object> data, long ttlMillis) {


        // 构建JWT
        String token = Jwts.builder()
                .addClaims(data)
                .setExpiration(new Date(System.currentTimeMillis()+expireTime)) // 设置token的过期时间
                .signWith(SignatureAlgorithm.HS256, signKey)
                .compact();

        return token;
    }

    // 解析token
    public static Claims parseJWT(String token) {
        Claims body = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(token)
                .getBody();
        return body;
    }
}
